package com.oneday.ashop.console.resource;

import com.oneday.ashop.core.base.mongo.BaseMongoDaoSupport;
import com.oneday.ashop.core.entity.Resource;
import org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 资源持久层
 * Created by admin on 2016/7/2.
 */
@Repository
public class ResourceDao extends BaseMongoDaoSupport {

    /**
     * 添加资源
     * @param resource
     */
    public void addResource(Resource resource) {
        insert(resource);
    }

    /**
     * 查找更新资源
     * @param resourceId
     * @param update
     */
    public void findAndModifyResource(Integer resourceId, Update update) {
        Query query = Query.query(Criteria.where("_id").is(resourceId));
        findAndModify(query, update, Resource.class);
    }

    /**
     * 查找资源
     * @return
     */
    public List<Resource> findAllResources() {
        Query query = new Query(Criteria.where("status").ne(Resource.STATUS_DELETE));
        return find(query, Resource.class);
    }
}
